//
//  LNTextView.m
//
//  Created by KuiYin on 15/11/5.
//  Copyright © 2015年 shenming. All rights reserved.
//

#import "LNTextView.h"

@interface LNTextView()<UITextViewDelegate>
{
    UILabel *_placeholderLabel;
    CGFloat _originalHeight; // 原始高度
    CGFloat _maxHeight; // 最高高度
    CGFloat _width;
    BOOL _flag; // 只赋值一次原始高度
}
@end

@implementation LNTextView

- (instancetype)init
{
    if (self = [super init]) {
        self.delegate = self;
        self.font = [UIFont systemFontOfSize:14];
        
        _placeholderLabel = [[UILabel alloc]init];
        _placeholderLabel.textColor = [UIColor lightGrayColor];
        [self addSubview:_placeholderLabel];
    }
    return self;
}

- (instancetype)initWithWidth:(CGFloat)width maxHeight:(CGFloat)maxHeight
{
    if (self = [self init]) {
        _width = width;
        _maxHeight = maxHeight;
        _flag = YES;
    }
    return self;
}

// 设置提示语
- (void)setPlaceholder:(NSString *)placeholder
{
    _placeholder = placeholder;
    
    _placeholderLabel.text = _placeholder;
}

// 设置提示语字体颜色
- (void)setPlaceholderColor:(UIColor *)placeholderColor
{
    _placeholderColor = placeholderColor;
    
    _placeholderLabel.textColor = placeholderColor;
}

// 记录TextView的原始高度
- (void)setFrame:(CGRect)frame
{
    // 如果是使用 initWithWidth:(CGFloat)width maxHeight:(CGFloat)maxHeight方法创建
    // 那么使用它的宽度 如果不是则使用原始frame
    if (_width > 0) {
        [super setFrame:CGRectMake(frame.origin.x, frame.origin.y, _width, frame.size.height)];
    } else {
        [super setFrame:frame];
    }
    
    // 只赋值一次原始高度
    if (_flag) {
        _originalHeight = frame.size.height;
        _flag = NO;
    }
}

// UITextView即将显示之前会执行这个方法
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self settingPlaceholderFrame];
}

- (void)settingPlaceholderFrame
{
    _placeholderLabel.font = self.font;
    CGSize placeholderSize = [_placeholder sizeWithAttributes:@{NSFontAttributeName:self.font}];
    _placeholderLabel.frame = (CGRect){{self.textContainerInset.left + 2,self.textContainerInset.top},placeholderSize};
}

- (void)textViewDidEndEditing:(UITextView *)textView
{    
    [self settingPlaceholderHidden:textView.text];
    
    if ([self.lnDelegate respondsToSelector:@selector(textViewDidEndEditing:)]) {
        [self.lnDelegate textViewDidEndEditing:self];
    }
}

- (void)textViewDidChange:(UITextView *)textView
{
    [self settingPlaceholderHidden:textView.text];
    
    // 使用initWithWidth...方法 创建
    if (_maxHeight > 0) {
        CGFloat height = [self calcTextViewHeightWithWidth:self.frame.size.width text:textView.text];

        BOOL flag = [self updateTextViewHeight:height];
        if (flag) {
            if ([_lnDelegate respondsToSelector:@selector(lnTextView:changedHeight:)]) {
                [_lnDelegate lnTextView:self changedHeight:height];
            }
        }
    }
    
    if ([self.lnDelegate respondsToSelector:@selector(textViewDidChange:)]) {
        [self.lnDelegate textViewDidChange:self];
    }
}

// 判断是否隐藏提示语
- (void)settingPlaceholderHidden:(NSString *)text
{
    NSString *str = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if(str.length <= 0) {
        _placeholderLabel.hidden = NO;
    } else {
        _placeholderLabel.hidden = YES;
    }
}

// 计算TextView的高度
- (CGFloat)calcTextViewHeightWithWidth:(CGFloat)width text:(NSString *)text
{
    UITextView *calcView = nil;
    if (calcView == nil) {
        calcView = [[UITextView alloc]init];
    }
    
    calcView.textContainerInset = self.textContainerInset;
    calcView.font = self.font;
    calcView.text = text;
    CGSize calcViewSize = [calcView sizeThatFits:CGSizeMake(width, CGFLOAT_MAX)];
    
    NSLog(@"height -- %f",calcViewSize.height);
    return calcViewSize.height;
}

// 修改TextView的Height
- (BOOL)updateTextViewHeight:(CGFloat)height
{
    height = MAX(_originalHeight, height);
    height = MIN(_maxHeight, height);
    self.scrollEnabled = height == _maxHeight;
    
    if (height == _originalHeight) return false;
    
    CGRect frame = self.frame;
    if (height != frame.size.height) {
        frame.size.height = height;
        self.frame = frame;
        
        return true;
    }
    return false;
}
@end
